package com.xpec.c4.service.module.common;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.player.Player;
import com.ms.player.PlayerNickNameParser;
import com.ms.service.module.common.message.SPlayerCommonInfo;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.game.utility.GameUtility;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.group.transportlayer.GroupMessage;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.ModuleName;
import com.xpec.c4.service.module.ServerProperties;
import com.xpec.c4.service.module.common.content.PlayerContent;
import com.xpec.c4.service.module.common.content.SessionContent;
import com.xpec.c4.service.module.common.db.CommonDAO;

/**
 * 
 * @author brian_yang
 *
 */
public class PlayerCommonInfoManager {
	
	/**
	 * Log管理器
	 */
	private static final Log log = LogFactory.getLog(PlayerCommonInfoManager.class);

	
	/**
	 * 
	 */
	private static PlayerCommonInfoManager instance = new PlayerCommonInfoManager();
	
	/**
	 * 
	 * @return
	 */
	public static PlayerCommonInfoManager getInstance(){
		return instance;
	}

	/**
	 * key: guid
	 * 由player guid 查 session
	 */
	private final Map<GUID, GUID> playersSession = new ConcurrentHashMap<GUID, GUID>();
	
	/**
	 * key: session
	 * 由session 查player guid
	 */
	private final Map<GUID, GUID> sessionPlayers = new ConcurrentHashMap<GUID, GUID>();
	
	/**
	 * key: guid
	 * 由player guid 查 name
	 */
	private final Map<GUID, String> guidName = new ConcurrentHashMap<GUID, String>();
	
	/**
	 * key: name
	 * 由name 查player guid
	 */
	private final Map<String, GUID> nameGuid = new ConcurrentHashMap<String, GUID>();
	
	/**
	 * key: player guid
	 * Value: zone id
	 */
	private final Map<GUID, String> guidZoneId = new ConcurrentHashMap<GUID, String>();
	
	/**
	 * key: player guid
	 * 由player guid 查 accountId
	 */
	private final Map<GUID, GUID> playerAccount = new ConcurrentHashMap<GUID, GUID>();
	
	/**
	 * 查詢session 對應的 content 
	 * key : session
	 * value :  @see SessionContent
	 */
	private final Map<GUID, SessionContent> sessionContentMap = new ConcurrentHashMap<GUID, SessionContent>();

	private CommonModule commonModule;
	
	/**
	 * 是一號Common Module才去更新redis
	 */
	private boolean isFirstCommonModule;
		

	/**
	 * 送出更新 session info 的要求到 common module<br>
	 * player 登入 zone 的時候呼叫
	 * 
	 * @param player 角色資料
	 */
	public static void sendUpdatePlayerSessionInfo(Player player){
		if (player == null) {
			return;
		}
		
		// 發送消息告訴其他服務器，记录阵营session关系
		SPlayerCommonInfo info = new SPlayerCommonInfo();
		info.type = SPlayerCommonInfo.UPDATE;
		info.guid = player.getGuid();
		info.session = player.getSessionID();
		info.userType = player.getUserType();
//		info.name = player.getName();
		info.name = PlayerNickNameParser.mergeNickName(player.getName(), player.getNameNumber());
		info.accountId = player.getAccountId();
		info.uplineCode = player.getUplineCode();
		info.trialAccount = player.isTrialAccount();
		info.verifiedMember = player.isVerifiedMember();
		
		GroupMessage msg = new GroupMessage();
		msg.setMessageContent(info);
		msg.setMessageType(MessageTypes.S_PLAYER_COMMON_INFO);
		msg.setDestModuleID(ModuleName.COMMON);
		
		MessageAction.sendGroupMessage(msg);		
	}

	/**
	 * 送出移除 session info 的要求到 common module
	 */
	public static void sendRemovePlayerSessionInfo(Player player, GUID sessionID) {
		if (player == null) {
			return;
		}

		// 發送消息告訴其他服務器，记录阵营session关系
		SPlayerCommonInfo info = new SPlayerCommonInfo();
		info.type = SPlayerCommonInfo.REMOVE;
		info.guid = player.getGuid();
		info.session = sessionID;
		info.userType = player.getUserType();
		info.accountId = player.getAccountId();
		info.name = player.getNameWithNumber();

		GroupMessage msg = new GroupMessage();
		msg.setMessageContent(info);
		msg.setMessageType(MessageTypes.S_PLAYER_COMMON_INFO);
		msg.setDestModuleID(ModuleName.COMMON);

		MessageAction.sendGroupMessage(msg);
	}
	
	/**
	 * 送出更新 zone id 的要求到 common module
	 * @param zoneId
	 */
	public static void sendUpdatePlayerZoneID(Player player, String zoneId){
		if (player == null) {
			return;
		}
		
		// 發送消息告訴其他服務器
		SPlayerCommonInfo info = new SPlayerCommonInfo();
		info.type = SPlayerCommonInfo.UPDATE_ZONE_ID;
		info.guid = player.getGuid();
		info.zoneId = zoneId;
		info.userType = player.getUserType();
		
		GroupMessage msg = new GroupMessage();
		msg.setMessageContent(info);
		msg.setMessageType(MessageTypes.S_PLAYER_COMMON_INFO);
		msg.setDestModuleID(ModuleName.COMMON);
		
		MessageAction.sendGroupMessage(msg);
	}
	
	
	public static void initRedisCommon(){
		CommonDAO.flushRedisCommon();
	}
	
	
	/**
	 * 基本上玩家上線的時候, 會來call 這個method
	 * @param guid
	 * @param session
	 * @param name
	 */
	public void updatePlayerCommonInfo(GUID accuontId, GUID guid, GUID session, String name, int userType) {

		if(log.isInfoEnabled() && ServerProperties.isSaveConsoleLog(userType)){
			log.info("STEP 3!!!!  updatePlayerCommonInfo  = > accuontId = " + accuontId +" | guid = " + guid + " | sessionId = " + session + " | name = " + name);
		}

		if(guid != null){
			playersSession.put(guid, session);
			guidName.put(guid, name);
			playerAccount.put(guid, accuontId);
			if (isFirstCommonModule) {
				PlayerContent playerContent = new PlayerContent();
				playerContent.setAccountId(accuontId.toString());
				playerContent.setName(name);
				playerContent.setSessionId(session.toString());
				CommonDAO.savePlayerContent(guid, playerContent);
			}
		}

		if(session != null){
			sessionPlayers.put(session, guid);
			if (isFirstCommonModule) {
				CommonDAO.addPlayerIdToSessionContent(guid, session);
			}
//			log.info("STEP 3-1!!!!sessionPlayers.put => PlayerName:" + name + "|playerId:" + guid + "|sessionId:" + session);
		}
		
		if(name != null){	
			nameGuid.put(name, guid);
			if (isFirstCommonModule) {
				CommonDAO.addNamePlayerPair(name, guid);
			}
		}

	}
	
	/**
	 * 基本上玩家下線的時候, 會來call 這個method, 移除一些資料
	 * name和guid的對應會保留
	 * @param guid
	 */
	public void removePlayerCommonInfo(GUID accountId, GUID playerId, GUID sessionID , String name){
		
 		if (log.isInfoEnabled()) {
			log.info("Session Close Log " +  GameUtility.getCurMethodName() + "! AccountId = " + accountId + " | PlayerId = " + playerId + " | sessionId = " + sessionID + " | Name = " + name);
		}
		
		//離線錯誤需測試修正
		String playerName = null;
		GUID playerGUID = null;
		
		if (sessionID != null) {
			sessionPlayers.remove(sessionID);
			sessionContentMap.remove(sessionID);

			if(isFirstCommonModule){
				CommonDAO.removeSessionContent(sessionID);
			}
		}
		
//		if(playerId == null){
//			if (log.isInfoEnabled()) {
//				log.info("Session Close Log " + GameUtility.getCurMethodName() + "! PlayerId is Null!!! AccountId = " + accountId + " | PlayerId = " + playerId + " | sessionId = " + sessionID + " | Name = " + name);
//			}
//		}
		
		if(playerId != null) { 
			playersSession.remove(playerId);
			playerAccount.remove(playerId);
			
			guidZoneId.remove(playerId);
			
			playerName = guidName.remove(playerId);	
			if(name != null){
				playerGUID = nameGuid.remove(name);	
			}
			
			if (isFirstCommonModule) {
				CommonDAO.removePlayerContent(playerId);
			}
		}
	
		if (playerName != null) {
			nameGuid.remove(playerName);
			if (isFirstCommonModule) {
				CommonDAO.removeNamePlayerPair(playerName);
			}
		}
		
		if (playerGUID != null) {
			guidName.remove(playerGUID);
		}
	}

	/**
	 * 
	 * @param guid
	 * @param zoneId
	 */
	public void updatePlayerZoneId(GUID guid, String zoneId){
		if (guid != null) {
			if (zoneId != null) {
				guidZoneId.put(guid, zoneId);
				if (isFirstCommonModule) {
					CommonDAO.addZoneIdToPlayerContent(guid, zoneId);
				}
			} else {
				guidZoneId.remove(guid);
				if (isFirstCommonModule) {
					CommonDAO.removeZoneIdByPlayer(zoneId, guid);
				}
			}
		}
	}
	
	
	/**
	 * 
	 * @param guid
	 * @return
	 */
	public String getPlayerZoneId(GUID guid){
		
		if (guid == null) {
			return null;
		}
	
		String zoneId = guidZoneId.get(guid);
		if (!GameUtility.isHaveString(zoneId)) {
			zoneId = CommonDAO.getZoneIdByPlayerId(guid);
			if(GameUtility.isHaveString(zoneId)){
				guidZoneId.put(guid, zoneId);
			}
		}
		return zoneId;
	}
	

	/**
	 * 
	 * @param guid
	 * @return
	 */
	public GUID getSessionByPlayerGuid(GUID guid){
		if(guid == null) {
			return null;
		}
		
		GUID sessionId = playersSession.get(guid);
		if(sessionId == null) {
			sessionId = CommonDAO.getSessionByPlayerId(guid);
			if(sessionId != null){
				playersSession.put(guid, sessionId);
			}
		}
		return sessionId;
	}
	
	
	/**
	 * 
	 * @param session
	 * @return
	 */
	public GUID getPlayerGuidBySession(GUID session){
		if(session == null)
		{
			return null;
		}
		
		GUID playerId = sessionPlayers.get(session);
		if(playerId == null){
			playerId = CommonDAO.getPlayerIdBySessionId(session);
			if(playerId != null){
				sessionPlayers.put(session, playerId);
			}
		}	
		return playerId;
	}
	
	
	/**
	 * 
	 * @param guid
	 * @return
	 */
	public String getPlayerNameByGuid(GUID guid){
		if (guid == null) {
			return null;
		}
		
		String name = guidName.get(guid);
		if(!GameUtility.isHaveString(name)){
			name = CommonDAO.getNameByPlayer(guid);
			if(GameUtility.isHaveString(name)){
				guidName.put(guid, name);
			}
		}
		return name;
	}
	

	/**
	 * 
	 * @param name
	 * @return
	 */
	public GUID getGuidByPlayerName(String name){
		if (name == null) {
			return null;
		}
		
		GUID playerId = nameGuid.get(name);
		if(playerId == null){
			playerId = CommonDAO.getPlayerByName(name);
			if(playerId != null){
				nameGuid.put(name, playerId); 
			}
		}
		return playerId;
	}
	
	
	/**
	 * 
	 * @param playerGuid
	 * @return
	 */
	public GUID getAccountIdByPlayerGuid(GUID playerGuid){
		if (playerGuid == null) {
			return null;
		}

		GUID accountId = playerAccount.get(playerGuid);
		if (accountId == null) {
			accountId = CommonDAO.getAccountIdByPlayerId(playerGuid);
			if(accountId != null){
				playerAccount.put(playerGuid, accountId);
			}
		}
		return accountId;
	}
	
	/**
	 * 取得session 對應的 content
	 */
	public SessionContent getSessionContent(GUID sessionId) {
		if(sessionId == null){
			return null;
		}
		
		SessionContent content = sessionContentMap.get(sessionId);
		if(content == null) {
			content = CommonDAO.getSessionContent(sessionId);
			if(content != null) {
				sessionContentMap.put(sessionId, content);
			}
		}
		return content;
	}
	
	
	/**
	 * 取得session所屬gateway
	 */
	public String getGatewayBySessionId(GUID sessionId) {
		SessionContent content =  getSessionContent(sessionId);
		if (content == null) {
			return null;
		}
		
		String gateWay = content.getGateway();
		if(!GameUtility.isHaveString(gateWay)) {
			gateWay = CommonDAO.getGateWayBySessionId(sessionId);
			if(GameUtility.isHaveString(gateWay)){
				getSessionContent(sessionId).setGateway(gateWay);
			}
		}
		return gateWay;
	}
	
	/**
	 * 連線時設定對應 session的一些資料
	 */
	public void setSessionContent(GUID sessionId, SessionContent sessionContent) {
		if (sessionId == null) {
			return;
		}

		if (sessionContent != null) {
			sessionContentMap.put(sessionId, sessionContent);
			//寫到redis
			CommonDAO.saveSessionContent(sessionId, sessionContent);
		} else {
			sessionContentMap.remove(sessionId);
			//寫到redis
			CommonDAO.removeSessionContent(sessionId);
		}
	}
	
	/**
	 * 用session Id 取得玩家登入的IP
	 * @param sessionId
	 * @return
	 */
	public String getClientIpBySessionId(GUID sessionId) {
		SessionContent content = getSessionContent(sessionId);
		if(content == null){
			return null;
		}
		
		String ip = content.getIp();
		if(!GameUtility.isHaveString(ip)){
			 ip = CommonDAO.getIPBySessionId(sessionId);
			 if(GameUtility.isHaveString(ip)){
				 getSessionContent(sessionId).setIp(ip);
			 }
		}
		return ip;
	}

	/**
	 * 用session Id 取得 玩家登入的平台
	 * @param sessionId
	 * @return
	 */
	public String getLoginPlatformBySessionId(GUID sessionId){
		SessionContent content =  getSessionContent(sessionId);
		if(content == null){
			return null;
		}
		
		String platForm =  content.getPlatform();
		if(!GameUtility.isHaveString(platForm)){
			platForm = CommonDAO.getPlatFormBySessionId(sessionId);
			if(GameUtility.isHaveString(platForm)){
				getSessionContent(sessionId).setPlatform(platForm);
			}
		}
		return platForm;
	}
	
	public void setCommonModule(CommonModule commonModule) {
		this.commonModule = commonModule;
		this.isFirstCommonModule = ServerProperties.getFirstModuleName(ModuleName.COMMON).equals(commonModule.getModuleName());
	}
	
	/**
	 * 這是當mobile common module的server重新啟動時,所有的player資訊都是空的,
	 * 因此在mobile shutdown時, 要保存目前common module中上線的玩家列表
	 * 在下次啟動時, 讀取上次shutdown所暫存的玩家列表
	 */
	public List<GUID> getOnlinePlayers() {
		if (playersSession.size() > 0) {
			List<GUID> onlinePlayerList = new ArrayList<GUID>();
			Set<GUID> playersSet = playersSession.keySet();
			for (GUID player : playersSet) {
				onlinePlayerList.add(player);
			}
			return onlinePlayerList;
		}
		return null;
	}
	
	public List<GUID> getOnlinePlayersSessions() {
		List<GUID> result = new ArrayList<GUID>(sessionPlayers.keySet());
		return result;
	}
	
	public int getPlayersCount(){
		return sessionPlayers.size();
	}

	
	
	@Deprecated
	public void updatePlayerGuidNameMapping(GUID guid , String name){
		if(guid == null || name == null)
		{
			return;
		}
		nameGuid.put(name, guid);
		guidName.put(guid, name);
	}
	
	
	public String getZoneIdBySession(GUID sessionId){
		if(sessionId == null) {
			return null;
		}
		
		GUID playerId = sessionPlayers.get(sessionId);
		if(playerId == null) {
			playerId = CommonDAO.getPlayerIdBySessionId(sessionId);
			if(playerId == null){
				return null;
			}else{
				sessionPlayers.put(sessionId, playerId);
			}
		}
		
		String zoneId = guidZoneId.get(playerId);
		if(!GameUtility.isHaveString(zoneId)){
			zoneId = CommonDAO.getZoneIdByPlayerId(playerId);
			if(GameUtility.isHaveString(zoneId)){ 
				guidZoneId.put(playerId, zoneId);
			}
		}
		return zoneId;
	}
	
	public String getZoneIdByPlayerId(GUID playerId){
		if(playerId == null){
			return null;
		}
		
		String zoneId = guidZoneId.get(playerId);
		if(!GameUtility.isHaveString(zoneId)){
			zoneId = CommonDAO.getZoneIdByPlayerId(playerId);
			if(GameUtility.isHaveString(zoneId)){ 
				guidZoneId.put(playerId, zoneId);
			}
		}
		return zoneId;
	}
	
	/**
	 * 送出更新 ChatData 的要求到 common module
	 * 更新channel內ID
	 * @param zoneId
	 */
	public static void sendAddChSessionInChannel(GUID playerId, GUID sessionID, int channelID, int userType){
		
		// 發送消息告訴其他服務器  第一次加入頻道
		SPlayerCommonInfo info = new SPlayerCommonInfo();
		info.type = SPlayerCommonInfo.JOIN_CHAT_CHANNEL;
		info.guid = playerId;
		info.session = sessionID;
		info.channelID = channelID;
		info.userType = userType;
		
		GroupMessage msg = new GroupMessage();
		msg.setMessageContent(info);
		msg.setMessageType(MessageTypes.S_PLAYER_COMMON_INFO);
		msg.setDestModuleID(ModuleName.COMMON);
		
		MessageAction.sendGroupMessage(msg);
	}
	
	/**
	 * 送出更新 ChatData 的要求到 common module
	 * 更新group內ID
	 * @param zoneId
	 */
	public static void sendJoinGroup(GUID sessionID, GUID playerID, String playerName, int channelID , String fascphoto){
		
		// 發送消息告訴其他服務器   加入群組
		SPlayerCommonInfo info = new SPlayerCommonInfo();
		info.type = SPlayerCommonInfo.JOIN_GROUP;
		info.session = sessionID;
		info.channelID = channelID;
		info.guid = playerID;
		info.name = playerName;
		info.facephoto = fascphoto;
		
		GroupMessage msg = new GroupMessage();
		msg.setMessageContent(info);
		msg.setMessageType(MessageTypes.S_PLAYER_COMMON_INFO);
		msg.setDestModuleID(ModuleName.COMMON);
		
		MessageAction.sendGroupMessage(msg);
	}
	
	/**
	 * 送出更新 ChatData 的要求到 common module
	 * 更新group內ID
	 * @param zoneId
	 */
	public static void sendLeaveGroup(GUID sessionID, GUID playerID, int channelID){
		
		// 發送消息告訴其他服務器  離開群組
		SPlayerCommonInfo info = new SPlayerCommonInfo();
		info.type = SPlayerCommonInfo.LEAVE_GROUP;
		info.session = sessionID;
		info.channelID = channelID;
		info.guid = playerID;
		
		GroupMessage msg = new GroupMessage();
		msg.setMessageContent(info);
		msg.setMessageType(MessageTypes.S_PLAYER_COMMON_INFO);
		msg.setDestModuleID(ModuleName.COMMON);
		
		MessageAction.sendGroupMessage(msg);
	}
	
	public static void sendCreateOwnChannel(GUID playerID, String playerName, String password, 
			int channelID, String name, boolean isPublic, long createTime, int maxMember){
		
		// 發送消息告訴其他服務器  創立自創頻道
		SPlayerCommonInfo info = new SPlayerCommonInfo();
		info.type = SPlayerCommonInfo.CREATE_OWN_CHANNEL;
		info.guid = playerID;
		info.password = password;
		info.channelID = channelID;
		info.name = name;
		info.ownerName = playerName;
		info.isPublic = isPublic;
		info.time = createTime;
		info.maxMember = maxMember;
		
		GroupMessage msg = new GroupMessage();
		msg.setMessageContent(info);
		msg.setMessageType(MessageTypes.S_PLAYER_COMMON_INFO);
		msg.setDestModuleID(ModuleName.COMMON);
				
		MessageAction.sendGroupMessage(msg);
	}
	
	public static void sendCloseOwnChannel(int channelID){
		// 發送消息告訴其他服務器  關閉私人頻道
		SPlayerCommonInfo info = new SPlayerCommonInfo();
		info.type = SPlayerCommonInfo.CLOSE_OWN_CHANNEL;
		info.channelID = channelID;
						
		GroupMessage msg = new GroupMessage();
		msg.setMessageContent(info);
		msg.setMessageType(MessageTypes.S_PLAYER_COMMON_INFO);
		msg.setDestModuleID(ModuleName.COMMON);
						
		MessageAction.sendGroupMessage(msg);
	}
	
	public static void sendCloseGroupChannel(int channelID){
		// 發送消息告訴其他服務器  關閉群組
		SPlayerCommonInfo info = new SPlayerCommonInfo();
		info.type = SPlayerCommonInfo.CLOSE_GROUP;
		info.channelID = channelID;
						
		GroupMessage msg = new GroupMessage();
		msg.setMessageContent(info);
		msg.setMessageType(MessageTypes.S_PLAYER_COMMON_INFO);
		msg.setDestModuleID(ModuleName.COMMON);
						
		MessageAction.sendGroupMessage(msg);
	}
	
	/**
	 * 更新群組最後使用時間
	 */
	public static void sendUpdateGroupLastTime(HashSet<Integer> groupIDs, long lastTime){
		
		// 發送消息告訴其他服務器  更新群組最後使用時間
		SPlayerCommonInfo info = new SPlayerCommonInfo();
		info.type = SPlayerCommonInfo.UPDATE_LAST_TIME;
		info.groupList = new ArrayList<Integer>(groupIDs);
		info.time = lastTime;
		
		GroupMessage msg = new GroupMessage();
		msg.setMessageContent(info);
		msg.setMessageType(MessageTypes.S_PLAYER_COMMON_INFO);
		msg.setDestModuleID(ModuleName.COMMON);
				
		MessageAction.sendGroupMessage(msg);
	}
	
	/**
	 * 更新群組最後一句話
	 */
	public static void sendUpdateGroupLastWord(Integer groupID, String lastWord){
		
		// 發送消息告訴其他服務器  更新群組最後一句話
		SPlayerCommonInfo info = new SPlayerCommonInfo();
		info.type = SPlayerCommonInfo.UPDATE_LAST_WORD;
		info.channelID = groupID;
		info.lastword = lastWord;
		
		GroupMessage msg = new GroupMessage();
		msg.setMessageContent(info);
		msg.setMessageType(MessageTypes.S_PLAYER_COMMON_INFO);
		msg.setDestModuleID(ModuleName.COMMON);
				
		MessageAction.sendGroupMessage(msg);
	}
	
	/**
	 * 更新其他chat內的群組資料，在線名單
	 */
	public static void sendOnlineAddGroup(HashSet<Integer> groupIDs, GUID sessionID, GUID playerID){
		
		// 發送消息告訴其他服務器  更新其他chat內的群組資料，在線名單
		SPlayerCommonInfo info = new SPlayerCommonInfo();
		info.type = SPlayerCommonInfo.ONLINE_ADD_IN_GROUPS;
		info.groupList = new ArrayList<Integer>(groupIDs);
		info.session = sessionID;
		info.guid = playerID;
		
		GroupMessage msg = new GroupMessage();
		msg.setMessageContent(info);
		msg.setMessageType(MessageTypes.S_PLAYER_COMMON_INFO);
		msg.setDestModuleID(ModuleName.COMMON);
						
		MessageAction.sendGroupMessage(msg);
		
	}
	
	/**
	 * 更新群組最早的離線玩家時間
	 */
	public static void sendUpdateGroupOfflineData(Integer groupID, GUID playerID, Long firstOffLineTime, boolean isTrue){
		
		// 發送消息告訴其他服務器  更新群組最早的離線玩家時間
		SPlayerCommonInfo info = new SPlayerCommonInfo();
		info.type = SPlayerCommonInfo.UPDATE_GROUP_OFFLINE_TIME;
		info.channelID = groupID;
		info.time = firstOffLineTime;
		info.guid = playerID;
		info.isTrue = isTrue;
		
		GroupMessage msg = new GroupMessage();
		msg.setMessageContent(info);
		msg.setMessageType(MessageTypes.S_PLAYER_COMMON_INFO);
		msg.setDestModuleID(ModuleName.COMMON);
						
		MessageAction.sendGroupMessage(msg);
		
	}
	
	/**
	 * 更新群組最後使用時間
	 */
	public static void sendUpdateGroupMaxMember(HashSet<Integer> groupIDs, int maxMember){
		
		// 發送消息告訴其他服務器  更新群組最大人數
		SPlayerCommonInfo info = new SPlayerCommonInfo();
		info.type = SPlayerCommonInfo.UPDATE_MAX_MEMBER_COUNT;
		info.groupList = new ArrayList<Integer>(groupIDs);
		info.maxMember = maxMember;
		
		GroupMessage msg = new GroupMessage();
		msg.setMessageContent(info);
		msg.setMessageType(MessageTypes.S_PLAYER_COMMON_INFO);
		msg.setDestModuleID(ModuleName.COMMON);
				
		MessageAction.sendGroupMessage(msg);
	}
	
	/**
	 * 刪除群組邀請資訊
	 */
	public static void sendCancelGroupInviteInfo(int groupID, GUID playerID, boolean leaveGroup){
		if (log.isInfoEnabled()) {
			log.info(GameUtility.getCurMethodName() + ": groupID=" + groupID + " playerID=" + playerID
					+ " leaveGroup=" + leaveGroup);
		}

		// 發送消息告訴其他服務器  刪除群組邀請資訊
		SPlayerCommonInfo info = new SPlayerCommonInfo();
		info.type = SPlayerCommonInfo.CANCEL_INVITE_INFO;
		info.channelID = groupID;
		info.guid = playerID;
		info.isTrue = leaveGroup;
		
		GroupMessage msg = new GroupMessage();
		msg.setMessageContent(info);
		msg.setMessageType(MessageTypes.S_PLAYER_COMMON_INFO);
		msg.setDestModuleID(ModuleName.COMMON);
				
		MessageAction.sendGroupMessage(msg);
	}
	
	/**
	 * 更新玩家受到邀請群組ID
	 */
	public static void sendUpdatePlayerInviteData(HashSet<Integer> groupIDs, GUID playerID){
		
		//發送消息告訴其他服務器  更新玩家受到邀請群組ID
		SPlayerCommonInfo info = new SPlayerCommonInfo();
		info.type = SPlayerCommonInfo.UPDATE_PLAYER_INVITE_DATA;
		info.groupList = new ArrayList<Integer>(groupIDs);
		info.guid = playerID;
		
		GroupMessage msg = new GroupMessage();
		msg.setMessageContent(info);
		msg.setMessageType(MessageTypes.S_PLAYER_COMMON_INFO);
		msg.setDestModuleID(ModuleName.COMMON);
				
		MessageAction.sendGroupMessage(msg);
	}
	
	/**********************************************************
	 * 聊天end
	 * Mike_Tung
	 *********************************************************/
	
	/**
	 * 設定連線是否在背景模式運行
	 * @param sessionID
	 * @param isBackgroundMode
	 */
	public void setSessionInBackgroundMode(GUID sessionID, boolean isBackgroundMode){
		if(sessionID == null)
			return;
		
		SessionContent sessionContent = getSessionContent(sessionID);
		if(sessionContent != null){
			//設定是否在背景模式
			sessionContent.setBackgroundMode(isBackgroundMode);
			setSessionContent(sessionID, sessionContent);
		}
	}
	
	/**
	 * 該連線是否在背景模式運行
	 * @param sessionID
	 * @return
	 */
	public boolean isSessionInBackgroundMode(GUID sessionID){
		if(sessionID == null)
			return false;
		
		SessionContent content = getSessionContent(sessionID);
		if(content != null){
			return content.isBackgroundMode();
		}
		else return false;
	}

	/**
	 * 廣播更新公頻
	 */
	public static void sendUpdatePublicChannel(boolean isRemove, ArrayList<Integer> channelIDs){
		SPlayerCommonInfo info = new SPlayerCommonInfo();
		info.type = SPlayerCommonInfo.UPDATE_PUBLIC_CHANNEL;
		info.isRemovePublicChannel = isRemove;
		info.channels = channelIDs;
		
		GroupMessage msg = new GroupMessage();
		msg.setMessageContent(info);
		msg.setMessageType(MessageTypes.S_PLAYER_COMMON_INFO);
		msg.setDestModuleID(ModuleName.COMMON);
		MessageAction.sendGroupMessage(msg);
	}
	
}
